import threading
from typing import *
from pathlib import Path
from abc import ABC,abstractmethod

class TaskUnit(ABC):
    @abstractmethod
    def reset(me) -> None: pass
    @abstractmethod
    def add(me, path:str) -> None: pass

class Tasks(threading.Thread):
    def __init__(me):
        threading.Thread.__init__(me)
        me.setDaemon(True)
        me.__n_all = 0
        me.__n_has_done = 0
        me.__dirs = []

    def has_run(me) -> bool: return me.is_alive()

    def start(me, taskunit:TaskUnit, fileexts:List[str], dirs:List[Path], on_done) -> None:
        me.__taskunit = taskunit
        me.__fileexts = fileexts
        me.__dirs = dirs
        me.__on_done = on_done
        super().start()

    def run(me) -> None:
        try:
            #
            for dirpath in me.__dirs:
                print(dirpath)
                for ext in me.__fileexts:
                    filepaths = list(dirpath.glob(f"*.{ext}"))
                    me.__n_all += len(filepaths)
                    for path in filepaths:
                        me.__taskunit.add(str(path.resolve()))
                        me.__n_has_done += 1
            #
            me.__on_done()
        except Exception as e:
            print(str(e))

    def all_nums(me) -> int: return me.__n_all

    def has_done_nums(me) -> int: return me.__n_has_done

